package com.sp.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lxh.smart.SmartUpload;
import org.lxh.smart.SmartUploadException;

import com.sp.model.NewsBean;
import com.sp.model.NewsBeanOper;

public class AddNewsServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddNewsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("----请求的编码为-----");
		System.out.println(request.getCharacterEncoding());
		//request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,utf-8");			
		SmartUpload smartUpload = new SmartUpload();
		smartUpload.initialize(this.getServletConfig(), request, response);
		smartUpload.setMaxFileSize(10000000);
		smartUpload.setTotalMaxFileSize(20000000);
		smartUpload.setAllowedFilesList("doc,txt,jpg,png,gif");
		try {
			smartUpload.setDeniedFilesList("jsp,html,,");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		String destFileName=null;
		//+(Math.random()*9000+1000)+"."+smartUpload.getFiles().getFile(0).getFileExt()		
		try {
			smartUpload.upload();;
			if(!smartUpload.getFiles().getFile(0).isMissing()){
			destFileName=File.separator+"uploadImg"+File.separator+System.currentTimeMillis()+(long)(Math.random()*9000+1000)+".jpg";
			smartUpload.getFiles().getFile(0).saveAs(destFileName);}
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}				
		NewsBean news = new NewsBean();
		if(destFileName!=null){
		news.setPicture(destFileName);}
		if(smartUpload.getRequest().getParameter("ntitle")!=null)
			news.setTitle(new String(smartUpload.getRequest().getParameter("ntitle").getBytes("gbk"),"utf-8"));					
		if(smartUpload.getRequest().getParameter("tid")!=null)
			news.setTid(Integer.valueOf(smartUpload.getRequest().getParameter("tid")));
		if(smartUpload.getRequest().getParameter("uname")!=null)
			news.setUname(new String(smartUpload.getRequest().getParameter("uname").getBytes("gbk"),"utf-8"));
		if(smartUpload.getRequest().getParameter("content")!=null)
			news.setContent(new String(smartUpload.getRequest().getParameter("content").getBytes("gbk"),"utf-8"));
		news.setAddTime(new Date(System.currentTimeMillis()));
		news.setUpdateTime(new Date(System.currentTimeMillis()));
		news.setInhome(0);
		news.setIsvouch(0);
		news.setIstop(0);
		if(smartUpload.getRequest().getParameter("checkbox1")!=null){
			String[] result =smartUpload.getRequest().getParameterValues("checkbox1");
			for(String s1:result){
				if(s1.equals("ishome"))
					news.setInhome(1);
				if(s1.equals("isvouch"))
					news.setIsvouch(1);
				if(s1.equals("istop"))
					news.setIstop(1);
			}
		}
		System.out.println(news.toString());
		NewsBeanOper newsBeanOper = new NewsBeanOper();
		newsBeanOper.addNews(news);
		PrintWriter pw = response.getWriter();
		String resultString = "<script>"+
				"alert(\"添加成功\");"+
				"window.location.href='GoToNewslistServlet';"+
				"</script>";
		pw.write(resultString);				
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
